<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>
        订单修改
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="../../../backstage/css/x-admin.css" media="all">

    <!--百度编辑器-->
<!--     <script src="./ueditor/ueditor.config.js"></script>
    <script src="./ueditor/ueditor.all.min.js"></script>
    <script src="./ueditor/lang/zh-cn/zh-cn.js"></script> -->
</head>

<body>
    <div class="x-nav">
        <span class="layui-breadcrumb">
          <a><cite>首页</cite></a>
          <a><cite>订单管理</cite></a>
          <a><cite>修改订单</cite></a>
        </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"  
        href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon" style="line-height:30px">ဂ</i>
        </a>
    </div>
<div class="x-body">
    <form class="layui-form layui-form-pane" action="" id="add">
        <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
            <div class="layui-tab-content" >
                <div class="layui-tab-item layui-show">
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>订单ID
                        </label>
                        <div class="layui-input-block">
                            <input id="id" type="text" name="id" value="" autocomplete="off" class="layui-input-add" disabled>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">修改受保人</label>
                        <div class="layui-input-inline">
                            <select id="insuredId" name="insuredId">
                                <option value="">请选择宠物</option>
                                <option value="猫">猫</option>
                                <option value="狗">狗</option>
                                <option value="兔">兔</option>
                            </select>
                        </div>
                    </div>
                        <div class="layui-form-item">
                    <label class="layui-form-label">修改受保宠物</label>
                    <div class="layui-input-inline">
                        <select id="petId" name="petId">
                            <option value="">请选择宠物</option>
                            <option value="猫">猫</option>
                            <option value="狗">狗</option>
                            <option value="兔">兔</option>
                        </select>
                    </div>
                            </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">价格</label>
                        <div class="layui-input-block">
                            <input id="price" type="number" name="price" value=""  required="" lay-verify="required" autocomplete="off"
                                   class="layui-input-add" oninput = "if(value>99999)value=99999;value=value.replace(/[^\d]/g,'')">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">积分</label>
                        <div class="layui-input-block">
                            <input id="credit" type="number" name="credit" value=""  required="" lay-verify="required" autocomplete="off"
                                   class="layui-input-add" oninput = "if(value>99999)value=99999;value=value.replace(/[^\d]/g,'')">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <input type="button" id="edit" class="layui-btn" lay-filter="edit" lay-submit="edit" value="确认修改">
                        </input>
                    </div>
                    <div style="height:100px;"></div>
                </div>
            </div>
        </div>
    </form>
</div>
    <script src="../../../backstage/js/jquery.min.js"></script>
<script src="../../../backstage/lib/layui/layui.js" charset="utf-8"></script>
<script src="../../../backstage/js/x-layui.js" charset="utf-8"></script>
<!-- <script type="text/javascript">
    //实例化编辑器
    //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
    UE.getEditor('content',{initialFrameWidth:600,initialFrameHeight:400,});
</script> -->
<script>
    layui.use(['laydate','element','layer','form'], function(){
        $ = layui.jquery;       //jquery
        laydate = layui.laydate;        //日期插件
        lement = layui.element();       //面包导航
        layer = layui.layer;        //弹出层
         var form = layui.form();
        //data.elem 被执行事件的元素DOM对象，一般为button对象
        //data.form 被执行提交的form对象，一般在存在form标签时才会返回
        //data.field 当前容器的全部表单字段，名值对形式：{name:value}
        $(document).ready(function() {
            //获取传递过来的localStorage
            var id = window.localStorage.getItem('msg');
            console.log(id);
            $.ajax({
                type: 'POST',
                url: '/bill/selectByBillId',
                dataType: "json",
                data: {billId: id},
                error: function () {
                    console.log("失败");
                },
                success: function (result) {

                    console.log(result);
                    var uid =result.msg.user.id;

                    $("#id").val(result.msg.id);
                    $("#price").val(result.msg.price);
                    $("#credit").val(result.msg.credit);

                    var insuredPetId=result.msg.pet.id;
                    var insuredPersonId=result.msg.insured.id;
                    var insuredPet=result.msg.pet.name;
                    var insuredPerson=result.msg.insured.name;

                    $.ajax({
                        type: 'POST',
                        url: '/pet/selectPets',
                        dataType: "json",
                        data: {uid: uid},
                        error: function () {
                            console.log("失败");
                        },
                        success: function (result) {
                            console.log(result);
                            var str = "";
                            var i = 0;
                            str+='<option value="'+insuredPetId+'">'+insuredPet+'</option>';
                            for (i = 0; i < result.length; i++) {
                                str += '<option value="' + result[i].id + '">' + result[i].name + '</option>';
                            }
                            $('#petId').html(str);
                            form.render('select');//每添加一次数据就更新一次渲染
                        }
                    });
                    $.ajax({
                        type: 'POST',
                        url: '/person/findAllPerson',
                        dataType: "json",
                        data: {uid: uid},
                        error: function () {
                            console.log("失败");
                        },
                        success: function (result) {
                            console.log(result);
                            var str = "";
                            var i = 0;
                            str+='<option value="'+insuredPersonId+'">'+insuredPerson+'</option>';
                            for (i = 0; i < result.length; i++) {
                                str += '<option value="' + result[i].id + '">' + result[i].name + '</option>';
                            }
                            $('#insuredId').html(str);
                            form.render('select');//每添加一次数据就更新一次渲染
                        }
                    });
                    form.verify({
                        price: function(value){
                            if(value.length<=0){
                                return '价格不能为空';
                            }
                        }
                    });
                    form.verify({
                        credit: function(value){
                            if(value.length<=0){
                                return '积分不能为空';
                            }
                        }
                    });

                    $("#edit").click(function () {
                        var id= $.trim( $("#id").val());
                        var insured= $.trim($("#insuredId").val()) ;
                        var pet = $.trim($("#petId").val());
                        var price=$.trim($("#price").val());
                        var credit=$.trim($("#credit").val());

                        console.log(pet);
                        console.log(insured);
                        if (price==""){
                            layer.msg('价格不能为空',{icon:5,time:2000});
                            return false;
                        }
                        if (credit==""){
                            layer.msg('积分不能为空',{icon:5,time:2000});
                            return false;
                        }
                        $.ajax({
                            type: 'POST',
                            url: '/bill/updateByAdmin',
                            dataType: "json",
                            data: {
                                billId: id,
                                insuredId:insured,
                                petId:pet,
                                price:price,
                                credit:credit
                            },
                            error: function () {
                                alert("失败");
                            },
                            success: function (data) {

                                if(data.error=="被保险人id不允许为空"){
                                alert("被保险人id不允许为空");
                                }else if(data.error=="被保险宠物不允许为空"){
                                    alert("被保险宠物不允许为空");
                                }else {
                                    console.log(data.msg);
                                    alert("修改成功")
                                }
                            }
                        });
                    });
                }
            });
        });
    });

</script>
</body>
</html>